<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-24 14:39:10
 * @Description: 脚本清理指定用户的订单账务
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 

 // $out_item=array(37820,38029,38030,38031,38159,38281,38282,38365,38366,38631,40362,40574,40575,40576,40577,41291,41292,41374,41396,41397,41398,41521,41737,41794,43069,43070,43071,43148,43509,43511,43510,43512,43595,43596,43640,43736,43737,43738,43872,43914,44016,44354,44793,44795,44794,44796,44886,45055,45057,45056,45058,45316,45317,45377,45381,45392,45393,46351,46352,46376,46377,46360,46363,46364,46378,46379,46583,46584,46807,46870,46871,46872,46873,47089,47090,47091,47279,47334,47335,47426,47571,47570,47575,47576,48234,48235,48236,48521,48522,48523,48655,49098,49178,49307,49308,49309,49324,49325);
/**1017,
              1117,
              1217,
              1317,
              1417,
              1018
 */
$user_id_arr=DB::select("SELECT DISTINCT pay_user_id FROM `orders` WHERE orders.created_at > '2016-06-01 00:00:00' and orders.place=2 AND orders.`status` IN (1017,1117,1217,1317,1417,1018)");
// $user_id_arr=DB::select("SELECT DISTINCT pay_user_id FROM `orders` WHERE orders.created_at > '2016-06-01 00:00:00' and orders.place=2 AND orders.`status` IN (1017,1117,1217,1317,1417,1018) and pay_user_id=1906");


//总共  94个
//循环处理每个 用户的记录
foreach ($user_id_arr as $_ui) {
  //查询该用户余额支付的金额
  $amount=DB::select("SELECT sum(order_pay_record.amount) as amount FROM `order_pay_record` 
    LEFT JOIN orders on orders.order_id=order_pay_record.order_id
    WHERE order_pay_record.`status`=1 and order_pay_record.pay_type=8 and orders.created_at > '2016-06-01 00:00:00' 
    AND orders.place = 2 AND orders.`status` IN (1017,1117,1217,1317,1417,1018) AND orders.pay_user_id=?",array($_ui->pay_user_id));
  //如果存在余额额支付的 先模拟一笔充值
  if($amount[0]->amount!=0){
    $arrr_id=Account::service_recharge($_ui->pay_user_id,1,1,$amount[0]->amount,'service',1,0);//充值记录
    // $aur=AcountUserRecharge::find($arrr_id);
    // $aur->implement_time=
    // JournalAccount::where()->update(array(""));
  }
  //查找该用户支付的顶顶那
  $order_info=DB::select("SELECT * FROM `orders` WHERE orders.created_at > '2016-06-01 00:00:00' and orders.place=2 AND orders.`status` IN (1017,1117,1217,1317,1417,1018) and pay_user_id=?",array($_ui->pay_user_id));
  //循环处理订单
  foreach($order_info as $order){
    DB::transaction(function () use ($order) {
      $user = Users::find($order->pay_user_id);
      $pay_array=OrderPay::where("order_id",$order->order_id)->get();
      foreach ($pay_array as $_pa) {
        $tran_no = date('Ymd',time()).Common::generateCode();
        $order_amount=$_pa->amount;
        $pay_type=$_pa->pay_type; 
        $_pa->tran_no=$tran_no;
        $_pa->save();

        if($pay_type== 1||$pay_type== 7){//现金 或者银行卡支付
            Account::record($order->pay_user_id,$order->shop_id,1,$tran_no,1,102,$order_amount,'订单结算充值,订单号:'.$order->order_id);
            //记录用户支出
            Account::record($order->pay_user_id,$order->shop_id,1,$tran_no,2,202,$order_amount,'订单结算支出,订单号:'.$order->order_id);
            //记录公司账户收入
            Account::record_base('1',$tran_no,1,102,$order_amount,'订单结算收入,订单号:'.$order->order_id);
            //记录订单账务关系
            OrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
        }
        if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额              
          $arr=AcountUserRecharge::getUseRecharge(0,$order->pay_user_id,$order_amount,2,"asc");                  
          foreach ($arr as $_arr) {
            //记录了用户的充值消费记录
            $aurs=new AcountUserRechargeSpend;
            $aurs->aurr_id=$_arr->id;
            $aurs->user_id=$order->pay_user_id;
            $aurs->buss_id=$_pa->id;
            $aurs->type=1;
            $aurs->amount=$_arr->user_amount;
            $aurs->status=1;
            $aurs->tran_no=$tran_no;
            $aurs->shop_id=$order->shop_id;
            $aurs->implement_time=date("Y-m-d H:i:s",time());
            $aurs->save();
            //更改充值记录消费的记录
            AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
            //记录个人 各种账户余额
            Account::record($order->pay_user_id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,202,$_arr->user_amount,'订单结算支出,订单号:'.$order->order_id);
            //记录公司账户收入
            $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
            Account::record_base($base_acount_num,$tran_no,1,102,$_arr->user_amount,'订单结算收入,订单号:'.$order->order_id);
          }
          OrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,)); 
        }
        if($user->type == 2 && $pay_type== 6 ){//企业挂账     
          if($order->verify_status=1&&$order->verify_time=='2017-02-28 15:30:30'){//如果是很久以前核销过得 不记账
            continue;
          }     
          Account::record($order->pay_user_id,0,3,$tran_no,2,202,$order_amount,'订单结算支出,订单号:'.$order->order_id);
          OrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));      
        }      
        OrderAccount::where("tran_no",$tran_no)->update(array("created_at"=>$order->pay_time));
        JournalAccount::where("tran_no",$tran_no)->update(array("created_at"=>$order->pay_time));   
      } 

      /**
       * @Author:      lyt
       * @DateTime:  2017-05-25 16:06:52
       * @Description: 改订单的退款单的逻辑
       */
      $ro=ReturnOrder::where("order_id",$order->order_id)->where("status",1)->first();//如果存在则
      if($ro){
        $ror=ReturnOrderRecord::where("return_order_id",$ro->id)->get();
        foreach ($ror as $_ror) {
          $tran_no = date('Ymd',time()).Common::generateCode();
          $order_amount=$_ror["amount"];
          $pay_type=$_ror["pay_type"];
          //各种进行判断
          if($pay_type== 1 || $pay_type== 7){//现金 或者银行卡支付
              Account::record($order->pay_user_id,$order->shop_id,1,$tran_no,1,116,$order_amount,'订单退单收入,订单退货单号:'.$ro->id.',订单号:'.$ro->order_id);
              //记录用户收入
              Account::record($order->pay_user_id,$order->shop_id,1,$tran_no,2,216,$order_amount,'订单退单支出,订单退货单号:'.$ro->id.',订单号:'.$ro->order_id);
              //记录公司账户支出
              Account::record_base('1',$tran_no,2,216,$order_amount,'订单退单支出,订单退货单号:'.$ro->id.',订单号:'.$ro->order_id);
              //记录订单账务关系
              OrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
          }
          if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空
              $op=OrderPay::where("order_id",$ro->order_id)->where("pay_type",8)->where("status",1)->first();
              $arr=AcountUserRechargeSpend::getUseRecharge($op->id,$order_amount,1);                  
              foreach ($arr as $_arr) {
                //记录了用户消费退款记录
                $aurs=new AcountUserRechargeSpendRe;
                $aurs->aurr_id=$_arr->aurr_id;
                $aurs->user_id=$order->pay_user_id;//谁使用
                $aurs->aurr_sp_id=$_arr->id;                      
                $aurs->buss_id=$_ror->id;
                $aurs->type=1;
                $aurs->amount=$_arr->user_amount;
                $aurs->status=1;
                $aurs->tran_no=$tran_no;
                $aurs->shop_id=$order->shop_id;
                $aurs->implement_time=date("Y-m-d H:i:s",time());
                $aurs->save();
                //更改充值记录
                DB::update('update acount_user_recharge_record set current_amount = current_amount+? where id = ?', array($_arr->user_amount,$_arr->aurr_id));
                //更改充值记录消费记录
                AcountUserRechargeSpend::where('id',$_arr->id)->update(array('return_amount'=> $_arr->return_amount +$_arr->user_amount,));
                //记录个人 各种账户余额
                $sac=AcountUserRecharge::find($_arr->aurr_id);
                Account::record($order->pay_user_id,$sac->shop_id,$_arr->acount_type,$tran_no,1,116,$_arr->user_amount,'订单退单收入,订单退货单号:'.$ro->id.',订单号:'.$ro->order_id);
                //记录公司账户支出
                $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                Account::record_base($base_acount_num,$tran_no,2,216,$order_amount,'订单退单支出,订单退货单号:'.$ro->id.',订单号:'.$ro->order_id);
              }
              OrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2));   
          }
          if($user->type == 2 && $pay_type== 6){//企业挂账  只记录 个人消费  财务收入 在应收收回 收回时 计算 应收和退款计算
              if($ro->verify_status=1&&$ro->verify_time=='2017-02-28 15:30:30'){//如果是很久以前核销过得 不记账
                continue;
              }
              Account::record($order->pay_user_id,0,3,$tran_no,1,116,$order_amount,'订单退单收入,订单退货单号:'.$ro->id.',订单号:'.$ro->order_id);
              OrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2));
          }
          OrderAccount::where("tran_no",$tran_no)->update(array("created_at"=>$ro->implement_time));
          JournalAccount::where("tran_no",$tran_no)->update(array("created_at"=>$ro->implement_time));
          OrderReturn::where("order_id",$order->order_id)->where("pay_type",$pay_type)->where("status",1)->update(array("created_at"=>$ro->implement_time));
          // $o_p=new OrderReturn;
          // $o_p->order_id=$order->order_id;
          // $o_p->pay_type=$pay_type;
          // $o_p->amount=$order_amount;
          // $o_p->status=1;
          // $o_p->create_by=$sc_user_id;
          // $o_p->tran_no=$tran_no;
          // $o_p->create_type=$create_type;
          // $o_p->save();
        }
      }     
    });
    Log::info("用户：".$_ui->pay_user_id." 订单 ：".$order->order_id." 完毕");
  }

  //查询该用户的 可退金额 退款 进行平账
  $uad_balance=UserAccountDeatails::where("user_id",$_ui->pay_user_id)->where("acount_type",1)->sum("balance");
  AcountUseRefund::return_amount($_ui->pay_user_id,1,0,$uad_balance,"service",1,0,"平账退款");
}




